<?php
/* food */
function um_food_options()
{
	global $pagenow;
	
	$parent_slug = 'edit.php?post_type=food';
	$options_name = 'um-food-options';
	$form_action = $parent_slug . '&page=' . $options_name . '&updated=true';
	
	um_update_options( 'food' );
	?>
	<div class="wrap">
		<script type="text/javascript">
			var um_image_header_field = '<?php echo $options_name; ?>[header-image-url]';
			var um_image_id_field = '<?php echo $options_name; ?>[header-image-id]';
		</script>
		<h2>Food Options</h2>
		<?php
		if ( isset($_GET['updated']) && 'true' == esc_attr($_GET['updated']) )
		{
			um_update_options_success_message( 'Food' );
		}
		?>
		<div id="poststuff">
			<form method="post" action="<?php echo admin_url( $form_action ); ?>">
				<?php 
				settings_fields( $options_name );
				
				if ( $pagenow == 'edit.php' && $_GET['page'] == $options_name )
				{
					?>
					<table class="form-table">
						<?php $options = get_option( $options_name );	?>
						<?php
						$remove_button_image = get_bloginfo('template_url') . '/images/button_cancel_256.png';
						$preview = ($options['header-image-url']) ? '<img src="' . $options['header-image-url'] . '" alt="" />' : '';
						$delete_button = ($options['header-image-url']) ? '<a class="delete_header_image" href="javascript:void(0);">Remove Image</a>' : '';
						?>
						<div id="um_options_container">
							
							<div class="section_container group">
								<div class="header_image_container">
									<label>Banner</label>
									<input type="text" id="header_image_url" name="<?php echo $options_name;?>[header-image-url]" value="<?php echo esc_attr( $options['header-image-url'] ); ?>" />
									<input type="hidden" name="<?php echo $options_name;?>[header-image-id]" value="<?php echo esc_attr( $options['header-image-id'] ); ?>" />
								</div>
								
								<input class="upload_header_image upload_button button-primary" type="button" name="upload" value="Browse File" />
								<p class="description">Please upload JPG, PNG or GIF images only. For best results, the image should be 1000px by 130px in size.</p>
								<br />
								
								<div class="preview">
									<?php 
									if ( $preview )
									{
										?>
										<?php echo $delete_button; ?>
										<p class="header-preview-image"><?php echo $preview; ?></p>
										<?php
									}
									?>
								</div>
							</div>
							<script type="text/javascript">var remove_button_image = '<?php echo $remove_button_image; ?>'; </script>
						</div>
					</table>
					<?php
				}
				?>
				<p class="submit">
					<input name="Submit" type="submit" class="button-primary" value="Save Changes" />
					<input type="hidden" name="um-food-settings-submit" value="Y" />
				</p>
			</form>
		</div>
		
	</div>
	<?php
}

/* travels */
function um_travel_activity_options()
{
	global $pagenow;
	
	$parent_slug = 'edit.php?post_type=travel-activity';
	$options_name = 'um-travel-activity-options';
	$form_action = $parent_slug . '&page=' . $options_name . '&updated=true';
	
	um_update_options( 'travel-activity' );
	?>
	<div class="wrap">
		<script type="text/javascript">
			var um_image_header_field = '<?php echo $options_name; ?>[header-image-url]';
			var um_image_id_field = '<?php echo $options_name; ?>[header-image-id]';
		</script>
		<h2>Travels and Activities Options</h2>
		<?php
		if ( isset($_GET['updated']) && 'true' == esc_attr($_GET['updated']) )
		{
			um_update_options_success_message( 'Travels and Activities' );
		}
		?>
		<div id="poststuff">
			<form method="post" action="<?php echo admin_url( $form_action ); ?>">
				<?php 
				settings_fields( $options_name );
				
				if ( $pagenow == 'edit.php' && $_GET['page'] == $options_name )
				{
					?>
					<table class="form-table">
						<?php $options = get_option( $options_name );	?>
						<?php
						$remove_button_image = get_bloginfo('template_url') . '/images/button_cancel_256.png';
						$preview = ($options['header-image-url']) ? '<img src="' . $options['header-image-url'] . '" alt="" />' : '';
						$delete_button = ($options['header-image-url']) ? '<a class="delete_header_image" href="javascript:void(0);">Remove Image</a>' : '';
						?>
						<div id="um_options_container">
							
							<div class="section_container group">
								<div class="header_image_container">
									<label>Banner</label>
									<input type="text" id="header_image_url" name="<?php echo $options_name;?>[header-image-url]" value="<?php echo esc_attr( $options['header-image-url'] ); ?>" />
									<input type="hidden" name="<?php echo $options_name;?>[header-image-id]" value="<?php echo esc_attr( $options['header-image-id'] ); ?>" />
								</div>
								
								<input class="upload_header_image upload_button button-primary" type="button" name="upload" value="Browse File" />
								<p class="description">Please upload JPG, PNG or GIF images only. For best results, the image should be 1000px by 130px in size.</p>
								<br />
								
								<div class="preview">
									<?php 
									if ( $preview )
									{
										?>
										<?php echo $delete_button; ?>
										<p class="header-preview-image"><?php echo $preview; ?></p>
										<?php
									}
									?>
								</div>
							</div>
							<script type="text/javascript">var remove_button_image = '<?php echo $remove_button_image; ?>'; </script>
						</div>
					</table>
					<?php
				}
				?>
				<p class="submit">
					<input name="Submit" type="submit" class="button-primary" value="Save Changes" />
					<input type="hidden" name="um-travel-activity-settings-submit" value="Y" />
				</p>
			</form>
		</div>
		
	</div>
	<?php
}


/* entertainment */
function um_entertainment_options()
{
	global $pagenow;
	
	$parent_slug = 'edit.php?post_type=entertainment';
	$options_name = 'um-entertainment-options';
	$form_action = $parent_slug . '&page=' . $options_name . '&updated=true';
	
	um_update_options( 'entertainment' );
	?>
	<div class="wrap">
		<script type="text/javascript">
			var um_image_header_field = '<?php echo $options_name; ?>[header-image-url]';
			var um_image_id_field = '<?php echo $options_name; ?>[header-image-id]';
		</script>
		<h2>Entertainment Options</h2>
		<?php
		if ( isset($_GET['updated']) && 'true' == esc_attr($_GET['updated']) )
		{
			um_update_options_success_message( 'Entertainment' );
		}
		?>
		<div id="poststuff">
			<form method="post" action="<?php echo admin_url( $form_action ); ?>">
				<?php 
				settings_fields( $options_name );
				
				if ( $pagenow == 'edit.php' && $_GET['page'] == $options_name )
				{
					?>
					<table class="form-table">
						<?php $options = get_option( $options_name );	?>
						<?php
						$remove_button_image = get_bloginfo('template_url') . '/images/button_cancel_256.png';
						$preview = ($options['header-image-url']) ? '<img src="' . $options['header-image-url'] . '" alt="" />' : '';
						$delete_button = ($options['header-image-url']) ? '<a class="delete_header_image" href="javascript:void(0);">Remove Image</a>' : '';
						?>
						<div id="um_options_container">
							
							<div class="section_container group">
								<div class="header_image_container">
									<label>Banner</label>
									<input type="text" id="header_image_url" name="<?php echo $options_name;?>[header-image-url]" value="<?php echo esc_attr( $options['header-image-url'] ); ?>" />
									<input type="hidden" name="<?php echo $options_name;?>[header-image-id]" value="<?php echo esc_attr( $options['header-image-id'] ); ?>" />
								</div>
								
								<input class="upload_header_image upload_button button-primary" type="button" name="upload" value="Browse File" />
								<p class="description">Please upload JPG, PNG or GIF images only. For best results, the image should be 1000px by 130px in size.</p>
								<br />
								
								<div class="preview">
									<?php 
									if ( $preview )
									{
										?>
										<?php echo $delete_button; ?>
										<p class="header-preview-image"><?php echo $preview; ?></p>
										<?php
									}
									?>
								</div>
							</div>
							<script type="text/javascript">var remove_button_image = '<?php echo $remove_button_image; ?>'; </script>
						</div>
					</table>
					<?php
				}
				?>
				<p class="submit">
					<input name="Submit" type="submit" class="button-primary" value="Save Changes" />
					<input type="hidden" name="um-entertainment-settings-submit" value="Y" />
				</p>
			</form>
		</div>
		
	</div>
	<?php
}

/* advocacy / charity */
function um_advocacy_charity_options()
{
	global $pagenow;
	
	$parent_slug = 'edit.php?post_type=advocacy-charity';
	$options_name = 'um-advocacy-charity-options';
	$form_action = $parent_slug . '&page=' . $options_name . '&updated=true';
	
	um_update_options( 'advocacy-charity' );
	?>
	<div class="wrap">
		<script type="text/javascript">
			var um_image_header_field = '<?php echo $options_name; ?>[header-image-url]';
			var um_image_id_field = '<?php echo $options_name; ?>[header-image-id]';
		</script>
		<h2>Travels and Activities Options</h2>
		<?php
		if ( isset($_GET['updated']) && 'true' == esc_attr($_GET['updated']) )
		{
			um_update_options_success_message( 'Advocacies and Charities' );
		}
		?>
		<div id="poststuff">
			<form method="post" action="<?php echo admin_url( $form_action ); ?>">
				<?php 
				settings_fields( $options_name );
				
				if ( $pagenow == 'edit.php' && $_GET['page'] == $options_name )
				{
					?>
					<table class="form-table">
						<?php $options = get_option( $options_name );	?>
						<?php
						$remove_button_image = get_bloginfo('template_url') . '/images/button_cancel_256.png';
						$preview = ($options['header-image-url']) ? '<img src="' . $options['header-image-url'] . '" alt="" />' : '';
						$delete_button = ($options['header-image-url']) ? '<a class="delete_header_image" href="javascript:void(0);">Remove Image</a>' : '';
						?>
						<div id="um_options_container">
							
							<div class="section_container group">
								<div class="header_image_container">
									<label>Banner</label>
									<input type="text" id="header_image_url" name="<?php echo $options_name;?>[header-image-url]" value="<?php echo esc_attr( $options['header-image-url'] ); ?>" />
									<input type="hidden" name="<?php echo $options_name;?>[header-image-id]" value="<?php echo esc_attr( $options['header-image-id'] ); ?>" />
								</div>
								
								<input class="upload_header_image upload_button button-primary" type="button" name="upload" value="Browse File" />
								<p class="description">Please upload JPG, PNG or GIF images only. For best results, the image should be 1000px by 130px in size.</p>
								<br />
								
								<div class="preview">
									<?php 
									if ( $preview )
									{
										?>
										<?php echo $delete_button; ?>
										<p class="header-preview-image"><?php echo $preview; ?></p>
										<?php
									}
									?>
								</div>
							</div>
							<script type="text/javascript">var remove_button_image = '<?php echo $remove_button_image; ?>'; </script>
						</div>
					</table>
					<?php
				}
				?>
				<p class="submit">
					<input name="Submit" type="submit" class="button-primary" value="Save Changes" />
					<input type="hidden" name="um-advocacy-charity-settings-submit" value="Y" />
				</p>
			</form>
		</div>
		
	</div>
	<?php
}


/* giveaways */
function um_giveaways_options()
{
	global $pagenow;
	
	$parent_slug = 'edit.php?post_type=giveaways';
	$options_name = 'um-giveaways-options';
	$form_action = $parent_slug . '&page=' . $options_name . '&updated=true';
	
	um_update_options( 'giveaways' );
	?>
	<div class="wrap">
		<script type="text/javascript">
			var um_image_header_field = '<?php echo $options_name; ?>[header-image-url]';
			var um_image_id_field = '<?php echo $options_name; ?>[header-image-id]';
		</script>
		<h2>Giveaways Options</h2>
		<?php
		if ( isset($_GET['updated']) && 'true' == esc_attr($_GET['updated']) )
		{
			um_update_options_success_message( 'Giveaways' );
		}
		?>
		<div id="poststuff">
			<form method="post" action="<?php echo admin_url( $form_action ); ?>">
				<?php 
				settings_fields( $options_name );
				
				if ( $pagenow == 'edit.php' && $_GET['page'] == $options_name )
				{
					?>
					<table class="form-table">
						<?php $options = get_option( $options_name );	?>
						<?php
						$remove_button_image = get_bloginfo('template_url') . '/images/button_cancel_256.png';
						$preview = ($options['header-image-url']) ? '<img src="' . $options['header-image-url'] . '" alt="" />' : '';
						$delete_button = ($options['header-image-url']) ? '<a class="delete_header_image" href="javascript:void(0);">Remove Image</a>' : '';
						?>
						<div id="um_options_container">
							
							<div class="section_container group">
								<div class="header_image_container">
									<label>Banner</label>
									<input type="text" id="header_image_url" name="<?php echo $options_name;?>[header-image-url]" value="<?php echo esc_attr( $options['header-image-url'] ); ?>" />
									<input type="hidden" name="<?php echo $options_name;?>[header-image-id]" value="<?php echo esc_attr( $options['header-image-id'] ); ?>" />
								</div>
								
								<input class="upload_header_image upload_button button-primary" type="button" name="upload" value="Browse File" />
								<p class="description">Please upload JPG, PNG or GIF images only. For best results, the image should be 1000px by 130px in size.</p>
								<br />
								
								<div class="preview">
									<?php 
									if ( $preview )
									{
										?>
										<?php echo $delete_button; ?>
										<p class="header-preview-image"><?php echo $preview; ?></p>
										<?php
									}
									?>
								</div>
							</div>
							<script type="text/javascript">var remove_button_image = '<?php echo $remove_button_image; ?>'; </script>
						</div>
					</table>
					<?php
				}
				?>
				<p class="submit">
					<input name="Submit" type="submit" class="button-primary" value="Save Changes" />
					<input type="hidden" name="um-giveaways-settings-submit" value="Y" />
				</p>
			</form>
		</div>
		
	</div>
	<?php
}

/*== SUBMIT ==*/
function um_update_options( $options_slug )
{
	if ( isset($_POST['um-'.$options_slug.'-settings-submit']) && $_POST['um-'.$options_slug.'-settings-submit'] == 'Y' )
	{
		$settings = $_POST['um-'.$options_slug.'-options'];
		$updated = update_option( 'um-'.$options_slug.'-options', $settings );
	}
}

/*== SUCCESS MESSAGE ==*/
function um_update_options_success_message( $options_page )
{
	$success_message = $options_page . ' options updated.';	
	echo '<div class="updated"><p>',  $success_message , '</p></div>';
}


/*== RESGISTER AND LOAD ==*/
function um_register_theme_options_pages()
{
	$settings_food = add_submenu_page( 'edit.php?post_type=food', 'Food Options', 'Options', 'edit_theme_options', 'um-food-options', 'um_food_options' );
	$settings_travel_activity = add_submenu_page( 'edit.php?post_type=travel-activity', 'Travels and Activities Options', 'Options', 'edit_theme_options', 'um-travel-activity-options', 'um_travel_activity_options' );
	$settings_entertainment = add_submenu_page( 'edit.php?post_type=entertainment', 'Entertainment Options', 'Options', 'edit_theme_options', 'um-entertainment-options', 'um_entertainment_options' );
	$settings_advocacy_charity = add_submenu_page( 'edit.php?post_type=advocacy-charity', 'Advocacies and Charities Options', 'Options', 'edit_theme_options', 'um-advocacy-charity-options', 'um_advocacy_charity_options' );
	$settings_giveaways = add_submenu_page( 'edit.php?post_type=giveaways', 'Giveaways Options', 'Options', 'edit_theme_options', 'um-giveaways-options', 'um_giveaways_options' );
}
add_action('admin_menu', 'um_register_theme_options_pages');